package app.system.service.impl;

import app.system.entity.SysUser;
import app.system.entity.SysUserRole;
import app.system.mapper.SysUserMapper;
import app.system.mapper.SysUserRoleMapper;
import app.system.service.SysUserService;
import app.utils.Result.AjaxResult;
import app.utils.SoleID;
import app.utils.VeDate;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Service
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;

    @Override
    public int saveUser(SysUser sysUser) {
        SysUser sysUser1 = new SysUser();
        SysUser sysUser2 = new SysUser();
        SysUserRole userRole = new SysUserRole();
        String id = SoleID.getId();
        userRole.setUser_id(id);
        userRole.setRole_id(222);
        userRoleMapper.saveUserRole(userRole);
        sysUser1.setUser_name(sysUser.getUser_name());
        sysUser2.setUser_emil(sysUser.getUser_emil());
        System.out.println(JSONObject.toJSONString(userRole));
        if (userMapper.selectUserByTage(sysUser1).size() != 0){
            return -1;
        }
        if (userMapper.selectUserByTage(sysUser2).size() != 0){
            return -2;
        }
        sysUser.setUser_creatdate(VeDate.dateToStrLong(new Date()));
        sysUser.setUser_id(id);
        System.out.println(JSONObject.toJSONString(sysUser));
        return userMapper.saveUser(sysUser);
    }

    @Override
    public List<SysUser> selectUserByTage(SysUser sysUser) {
        return userMapper.selectUserByTage(sysUser);
    }

    @Override
    public SysUser selectUserByUserName(String user_name) {
        return userMapper.selectUserByUserName(user_name);
    }

    @Override
    public List<SysUser> getUserAll() {
        return userMapper.getUserAll();
    }

    @Override
    public int updateUser(SysUser sysUser) {
        return userMapper.updateUser(sysUser);
    }

    @Override
    public int deleteUser(SysUser sysUser) {
        return userMapper.deleteUser(sysUser);
    }

    /**
     * 判断用户是否登录
     * @return
     */
    @Override
    public int IfLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        SysUser sysUser = (SysUser) session.getAttribute("userInfo");
        if (sysUser == null){
            return -1;
        }else {
            return 0;
        }
    }
}
